﻿--[[
   @brief    GameTipsExpand
   @author   hanmanhuang
   @version  1.0
   @date     2016-04-25
--]]

base_utility.log("loading gametips.lua");


NUIGameTipsExpandEventListener = {

};

GameTipsExpand = {
    tips_dialog_id_ = 0;
    tips_dialog_ = nil;
    is_tick_to_hide = false;
    is_hover_ = false;
    current_tips_ = nil;
};

function GameTipsExpand:init()
    if GameTipsExpand.tips_dialog_ ~= nil then
       return;
    end;

    local main_uilib = ui_utility.uilib();
    if main_uilib == nil then
        base_utility.log("GameTipsExpand get main_uilib failure!");
        return;
    end;

    local main_wnd_id = main_uilib:GetControlIDbyCustomID(CID_MAINFRAME_Dialog_MainFrame);
    GameTipsExpand.tips_dialog_id_ = layout_mgr.restore_sub_layout(main_wnd_id, 
                                        "wegame_bannerlist\\dlgGameTips.xml");

    if GameTipsExpand.tips_dialog_id_ == 0 then
        base_utility.log("create tips dialog id error!!!");
        return;
    end;

    -- 标题
    GameTipsExpand.title_id_ = ui_utility.get_ctrl_id_by_path(GameTipsExpand.tips_dialog_id_, "lblTips");
    GameTipsExpand.title_ptr_ = ui_utility.get_ctrl_ptr_by_id(GameTipsExpand.title_id_);

    -- 图片
    GameTipsExpand.image_id_ = ui_utility.get_ctrl_id_by_path(GameTipsExpand.tips_dialog_id_, "btnThumb");
    GameTipsExpand.image_ptr_ = ui_utility.get_ctrl_ptr_by_id(GameTipsExpand.image_id_);

    -- mask
    GameTipsExpand.mask_id_ = ui_utility.get_ctrl_id_by_path(GameTipsExpand.tips_dialog_id_, "pnlMask");
    GameTipsExpand.mask_ptr_ = ui_utility.get_ctrl_ptr_by_id(GameTipsExpand.mask_id_);

    -- 关闭按钮
    GameTipsExpand.close_btn_id_ = ui_utility.get_ctrl_id_by_path(GameTipsExpand.tips_dialog_id_, "btnClose");
    GameTipsExpand.close_btn_ptr_ = ui_utility.get_ctrl_ptr_by_id(GameTipsExpand.close_btn_id_);

    local tips_dialog_ptr = main_uilib:GetControlPtr(GameTipsExpand.tips_dialog_id_);
    GameTipsExpand.tips_dialog_ = tolua.cast(tips_dialog_ptr, "Tenio::ITDialog");
    if GameTipsExpand.tips_dialog_ ~= nil then
        GameTipsExpand.tips_dialog_:LockMove(TRUE,TRUE);
    end
end;

function GameTipsExpand:show_tips_expand(is_show, x_offset, y_offset)
    local show = FALSE;
    if is_show then
        show = TRUE;
    end

    -- 初始化
    GameTipsExpand:init();

    -- 删除定时器
    GameTipsExpand:remove_tick();

    if GameTipsExpand.tips_dialog_ ~= nil then
        if show == TRUE then
            GameTipsExpand:move_to_position(x_offset, y_offset);
            GameTipsExpand.tips_dialog_:SetFocus();

            -- 曝光量QOS上传
            GameTipsExpand.sendshown_to_qos();
        end
        GameTipsExpand.tips_dialog_:Show(show);
    else
        base_utility.log("create tips dialog error!!!");
    end;        
end;

-- 上传Normal界面GameTips曝光量QOS
function GameTipsExpand:sendshown_to_qos()
    local b_will_send = false;
    if nil ~= GameTipsExpand.current_tips_ then
        local dw_gameid = GameTipsExpand.current_tips_.iGame_id;
        local dw_msgid = GameTipsExpand.current_tips_.dwMsg_id;

        if nil ~= banner_model.game_tips_shown and
                    table.getn(banner_model.game_tips_shown) > 0 then
            local length = table.getn(banner_model.game_tips_shown);
            local b_has_saved = false;
            local gameid_index = 1;
            for i = 1, length do
                if dw_gameid == banner_model.game_tips_shown[i].saved_gameid then
                    b_has_saved = true;
                    gameid_index = i;
                    break;
                end;
            end;
            if b_has_saved == false then
                -- 没有写过该游戏ID
                b_will_send = true;
                local param = {};
                param.saved_gameid = dw_gameid;
                param.saved_msgid = dw_msgid;
                param.saved_bigshow = 1;
                banner_model.game_tips_shown[length + 1] = param;
            else
                -- 写过，判断big_show字段
                local has_bigshow = banner_model.game_tips_shown[gameid_index].saved_bigshow;
                if nil ~= has_bigshow and 1 == has_bigshow then
                    -- 显示过，判断msgid
                    local shown_msgid = banner_model.game_tips_shown[gameid_index].saved_msgid;
                    if nil ~= shown_msgid and dw_msgid == shown_msgid then
                        b_will_send = false;
                    else
                        banner_model.game_tips_shown[gameid_index].saved_msgid = dw_msgid;
                        b_will_send = true;
                    end;
                else
                    -- 没有显示过
                    banner_model.game_tips_shown[gameid_index].saved_bigshow = 1;
                    banner_model.game_tips_shown[gameid_index].saved_msgid = dw_msgid;
                    b_will_send = true;
                end;
            end;
             
        else
            base_utility.log("[GameTipsExpand:sendshown_to_qos]game_tips_shown is nil");
            banner_model.game_tips_shown = {};
            local param = {};
            param.saved_gameid = dw_gameid;
            param.saved_msgid = dw_msgid;
            param.saved_bigshow = 1;
            banner_model.game_tips_shown[1] = param;
            b_will_send = true;
        end;

        if b_will_send == true then
            -- 发送曝光量qos
            tqos_mgr.report_game_tips(GameTipsExpand.current_tips_.iGame_id, 0, 0, GameTipsExpand.current_tips_.dwMsg_id);
        end;
    else
        base_utility.log("[GameTipsExpand:sendshown_to_qos]get tips info failed");
    end;
end;

function GameTipsExpand:move_to_position(x_offset, y_offset)
    if x_offset == nil then
        x_offset = 0;
    end

    if y_offset == nil then
        y_offset = 0;
    end

    if GameTipsExpand.tips_dialog_ then
        local   pTemp = GameTipsExpand.tips_dialog_:GetParent();
        local   rtDlg = GameTipsExpand.tips_dialog_:GetRectEx(TRUE,TRUE);

        if (nil ~= pTemp) then
            local   rtWnd = pTemp:GetRectEx(TRUE,TRUE);
            y_offset = y_offset - rtWnd.top;
            x_offset = x_offset - rtWnd.left;
        end;

        local w = rtDlg.right - rtDlg.left;
        local h = rtDlg.bottom - rtDlg.top;

        rtDlg.left   = x_offset;
        rtDlg.right  = rtDlg.left + w;
        rtDlg.top    = y_offset;
        rtDlg.bottom = rtDlg.top + h;

        GameTipsExpand.tips_dialog_:MoveEx(rtDlg,TRUE);
    end;    
end;

function GameTipsExpand:set_tips_info(tips)
    -- 初始化
    GameTipsExpand:init();

    -- 设置图片
    local image = nil;
    if tips.szUrl ~= nil then
        local tips_table = base_utility.json_to_table(tips.szUrl);
        base_utility.log("set_tips_info,tips url: "..tips.szUrl);
        if tips_table ~= nil and tips_table.tips_image ~= nil then
            image = tips_table.tips_image;
        end
    else
        base_utility.log("set_tips_info,tips url is nil");
    end

    if GameTipsExpand.image_id_ ~= nil then
        local url = "COLOR:RGB:0,0,0"; 
        if image ~= nil then
            url = "ONEIMAGE:newui_banner\\banner_tips_loading.gif|"..image..";Stretch;null;";    
            base_utility.log("[set_tips_info]bg url is"..url);
        else
            base_utility.log("set_tips_info,image url is nil");
        end

        ui_utility.setproperty_by_id(GameTipsExpand.image_id_, "Background", url);
    end

    -- 设置标题
    if GameTipsExpand.title_id_ ~= nil then
        ui_utility.setproperty_by_id(GameTipsExpand.title_id_, 'Caption', tips.szMsg);
    end

    -- 设置关闭按钮
    if GameTipsExpand.close_btn_ptr_ ~= nil then
        if tips.szDis_type == "Always" then
            GameTipsExpand.close_btn_ptr_:Show(FALSE);
        else
            GameTipsExpand.close_btn_ptr_:Show(TRUE);
        end
    end

    GameTipsExpand.current_tips_ = tips;
end;

-- 隐藏窗口倒计时
function GameTipsExpand:tick_to_hide()
    if GameTipsExpand.is_tick_to_hide == false and
       GameTipsExpand.is_hover_ == false then
        base_utility.log("GameTipsExpand:tick_to_hide");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval("GameTipsExpand.tick_count", 500);
        GameTipsExpand.is_tick_to_hide = true;
    end;
end;

function GameTipsExpand:remove_tick()
    base_utility.log("GameTipsExpand:remove_tick");
    if GameTipsExpand.is_tick_to_hide then 
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval("GameTipsExpand.tick_count");
    end
    GameTipsExpand.is_tick_to_hide = false;
end;

function GameTipsExpand:tick_count()
     base_utility.log("GameTipsExpand:tick_count");
     GameTipsExpand:show_tips_expand(false);
end;

function GameTipsExpand:set_hover_on_banner(hover)
     GameTipsExpand.is_hover_on_banner_ = hover;
end;

function GameTipsExpand:close_tips()
    base_utility.log("GameTipsExpand:close_tips");
    GameTipsExpand:show_tips_expand(false);
    local gamelib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    gamelib:recordGameTipViewed(GameTipsExpand.current_tips_.dwMsg_id, GameTipsExpand.current_tips_.iGame_id);
    if GameTipsExpand.current_tips_.szDis_type ~= "Always" then
        local banner = WGBannerList:get_banner(GameTipsExpand.current_tips_.iGame_id);
        if nil ~= banner then
            banner:set_game_tips_read();
            banner:show_game_tips_red_point(FALSE);
        end;
    end;
end;

function GameTipsExpand:click_tips()
    base_utility.log("GameTipsExpand:click_tips");
    GameTipsExpand:show_tips_expand(false);

    --发出事件到 open_link
    web_utility.open_link(GameTipsExpand.current_tips_.szUrl);

    local gamelib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    gamelib:recordGameTipViewed(GameTipsExpand.current_tips_.dwMsg_id, GameTipsExpand.current_tips_.iGame_id);

    -- 上报QOS
    tqos_mgr.report_game_tips(GameTipsExpand.current_tips_.iGame_id, 0, 1, GameTipsExpand.current_tips_.dwMsg_id);

    if GameTipsExpand.current_tips_.szDis_type ~= "Always" then
        local banner = WGBannerList:get_banner(GameTipsExpand.current_tips_.iGame_id);
        if nil ~= banner then
            banner:set_game_tips_read();
            banner:show_game_tips_red_point(FALSE);
        end;
    end;
end;


function NUIGameTipsExpandEventListener.on_kill_focus(uilib, dwControlID, dwNewControlID)
    base_utility.log("GameTipsExpand:lost focus");
    if GameTipsExpand.tips_dialog_ ~= nil and
       GameTipsExpand.tips_dialog_:IsMyOffspring(dwNewControlID) == TRUE then
        if dwNewControlID == GameTipsExpand.close_btn_id_ then
            GameTipsExpand:close_tips();
        elseif dwNewControlID == GameTipsExpand.mask_id_ then
            GameTipsExpand:click_tips();
        end
    else
        GameTipsExpand:show_tips_expand(false);
    end
end;

function NUIGameTipsExpandEventListener.on_mouse_hover(uilib, control_id)
    base_utility.log("GameTipsExpand:on_mouse_hover");
    GameTipsExpand.is_hover_ = true;
    GameTipsExpand:remove_tick();
end;

function NUIGameTipsExpandEventListener.on_mouse_leave(uilib, control_id)
    base_utility.log("GameTipsExpand:on_mouse_leave");
    GameTipsExpand.is_hover_ = false;
    if GameTipsExpand.is_hover_on_banner_ == false then
        GameTipsExpand:tick_to_hide();
    end
end;